import pandas as pd
import numpy as np

dates = pd.date_range("20210101", periods=4)
df = pd.DataFrame(np.ones((4, 5)) * 0, index=dates, columns=["a", "b", "c", "d", "e"])
df2 = pd.DataFrame(np.ones((4, 5)) * 1, index=dates, columns=["a", "b", "c", "d", "e"])
df3 = pd.DataFrame(np.ones((4, 5)) * 2, index=dates, columns=["a", "b", "c", "d", "e"])

print(df3)

# 上下合并, ignore_index指定是否要重新编号
res = pd.concat([df, df2, df3], axis=0, ignore_index=True)
print(res)


df1 = pd.DataFrame(np.ones((4, 5)) * 1, index=[1, 2, 3, 4], columns=["a", "b", "c", "d", "e"])
df2 = pd.DataFrame(np.ones((4, 5)) * 2, index=[2, 3, 4, 5], columns=["b", "c", "d", "e", "f"])

# 行列编号不一致的合并，默认是outer方式合并
res = pd.concat([df1, df2], join="outer")
print(res)

# 行列编号不一致的合并，指定inner, 本质上还是行的叠加，只是在列上取了相同的列名，不一致的列直接丢掉
res = pd.concat([df1, df2], join="inner", ignore_index=True)
print(res)


# ************** 指定join方向 *****************
df1 = pd.DataFrame(np.ones((4, 5)) * 1, index=[1, 2, 3, 4], columns=["a", "b", "c", "d", "e"])
df2 = pd.DataFrame(np.ones((4, 5)) * 2, index=[2, 3, 4, 5], columns=["b", "c", "d", "e", "f"])
res = pd.concat([df1, df2], axis=1, names=[1, 2, 3])
print(res)

# ************** append *****************
df = pd.DataFrame(np.ones((4, 5)) * 0, index=dates, columns=["a", "b", "c", "d", "e"])
df2 = pd.DataFrame(np.ones((4, 5)) * 1, index=dates, columns=["a", "b", "c", "d", "e"])
df3 = pd.DataFrame(np.ones((4, 5)) * 1, index=dates, columns=["a", "b", "c", "d", "e"])
res = df.append([df2, df3], ignore_index=True)
print(res)

s1 = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
res = df.append(s1, ignore_index=True)
print(res)
